package com.zt.java.back;

import com.sun.org.apache.xpath.internal.SourceTree;

class service{
//    private Boolean isContinueRun = true;
//    public void runMethod() {
//        String anyString = new String();
//        long l = 0;
//        while (isContinueRun == true) {
//            synchronized (isContinueRun) {
//
//            }
//            System.out.println("停下来了");
//        }
//    }
//    public void stopMethod(){
//        isContinueRun = false;
//    }
    String lock ;
    public service(String lock){
        this.lock = lock;

    }
    public void sleepMethod(){
        synchronized (lock){
            try {
                System.out.println("sleep -start");
                Thread.currentThread().sleep(20000);
                System.out.println("sleep -end");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    public void waitMethod(){
        synchronized (lock){
            try {
                System.out.println("wait - start");
                lock.wait();
                System.out.println("wait - end");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}
class  ThreadA extends Thread{
    private service service;
    public ThreadA(service service){
        this.service = service;
    }
    @Override
    public  void run(){
        service.sleepMethod();
    }
}
class  ThreadB extends Thread{
    private service service;
    public ThreadB(service service){
        this.service = service;
    }
    @Override
    public  void run(){
        service.waitMethod();
    }
}

public class ThreadDemo {
    public  static  void main(String args[]){
        String lock = new String("sdf");
        service service = new service(lock);
        ThreadA a = new ThreadA(service);
        a.start();
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        ThreadB b = new ThreadB(service);
        b.start();
    }


}
